package com.rzico.sbl.ui.report.dispatch

import android.text.InputFilter
import com.jeremyliao.liveeventbus.LiveEventBus
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityReportDispatchPayBinding
import com.rzico.sbl.model.DispatchPay
import com.xinnuo.common.extend.content
import com.xinnuo.common.extend.formatDecimal
import com.xinnuo.common.extend.oneClick
import com.xinnuo.common.extend.textDouble
import com.xinnuo.common.extend.textInt
import com.xinnuo.common.helper.*
import com.xinnuo.common.other.DecimalNumberFilter
import com.xinnuo.common_ui.base.BaseActivity

class ReportDispatchPayActivity : BaseActivity(R.layout.activity_report_dispatch_pay) {

    private val mBinding by lazy { ActivityReportDispatchPayBinding.bind(getRootView()) }

    private lateinit var mItemData: DispatchPay

    override fun initData() {
        initTitle(title = "收款编辑")
    }

    override fun initListener() {
        mItemData = intent.getParcelableExtra("data")!!

        mBinding.apply {
            mItemData.also {
                payOut.setText(it.quantity)
                payPrice.setText(it.price.formatDecimal())
                payMemo.setText(it.memo1)

                if (it.free.intValue > 0) payFree.setText(it.free)
                if (it.swap.intValue > 0) payOmit.setText(it.swap)
                if (it.coupon.doubleValue > 0) payCoupon.setText(it.coupon)

                if (it.exchange.intValue > 0) {
                    payTicket.setText(it.exchange)
                    payAmount.setText(it.exchangePrice.formatDecimal())
                }
            }

            payPrice.filters = arrayOf(DecimalNumberFilter(), InputFilter.LengthFilter(8))
            payAmount.filters = arrayOf(DecimalNumberFilter(), InputFilter.LengthFilter(8))
            payCoupon.filters = arrayOf(DecimalNumberFilter(), InputFilter.LengthFilter(8))

            btSave.oneClick {
                when {
                    payTicket.text.isNotEmpty() && payAmount.text.isEmpty() -> {
                        showToast("请输入水票抵扣金额")
                        payAmount.requestFocus()
                        return@oneClick
                    }
                    payTicket.text.isEmpty() && payAmount.text.isNotEmpty() -> {
                        showToast("请输入水票抵扣张数")
                        payTicket.requestFocus()
                        return@oneClick
                    }
                }

                mItemData.also {
                    it.quantity = payOut.content
                    it.price = payPrice.content.formatDecimal()
                    it.memo1 = payMemo.content
                    it.free = "0"
                    it.swap = "0"
                    it.coupon = "0.00"
                    it.exchange = "0"
                    it.exchangePrice = "0.00"

                    if (payFree.textInt > 0) it.free = payFree.content
                    if (payOmit.textInt > 0) it.swap = payOmit.content
                    if (payCoupon.textDouble > 0)
                        it.coupon = payCoupon.content.formatDecimal()

                    if (payTicket.textInt > 0) {
                        it.exchange = payTicket.content
                        it.exchangePrice = payAmount.content.formatDecimal()
                    }

                    it.subPrice =
                        ((it.quantity.intValue
                                - it.free.intValue
                                - it.swap.intValue
                                ) * it.price.doubleValue
                                - it.exchangePrice.doubleValue
                                - it.coupon.doubleValue
                                ).formatDecimal()

                    it.memo =
                        "(${
                            it.quantity.intValue
                        } - ${
                            it.free.intValue
                        } - ${
                            it.swap.intValue
                        }) * ${
                            it.price
                        } - ${
                            it.exchangePrice.doubleValue
                        } - ${
                            it.coupon.doubleValue
                        } = ${it.subPrice}"
                }

                intent
                    .getIntExtra("index", -1)
                    .also {
                        mItemData.position = it
                        LiveEventBus
                            .get("dispatchSecond")
                            .post(mItemData)
                    }

                finishView()
            }
        }
    }

}